package com.wps.wdztv6;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Timestamp;
import java.util.Date;

public class Coder {


    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        //dataSource.setJdbcUrl("jdbc:p6spy:sqlite:wdztv6/identifier.sqlite");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/wps?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");

        dataSource.setUsername("root");
        dataSource.setPassword("lhl123");

        //创建配置内容，两种风格都可以。
        //GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //数据库时间类型映射转换注册
        JdbcTypeMapping.registerMapping(Timestamp.class, Date.class);
        JdbcTypeMapping.registerMapping(java.sql.Date.class, Date.class);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        // 指定输出目录
        globalConfig.setSourceDir(projectPath + "/wdztv6/src/main/java");
        //设置根包
        globalConfig.setBasePackage("com.wps.test");

        //设置表前缀和只生成哪些表
        globalConfig.setGenerateSchema("flex");
        //globalConfig.setTablePrefix("tb_");

        //  >>>>>>>生成的表
        globalConfig.setGenerateTable("table_tetete_kakak");

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.setColumnConfig("table_tetete_kakak", columnConfig);


        return globalConfig;
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setSourceDir(projectPath + "/wdztv6/src/main/java");
        //globalConfig.setTableCommentFormat( UnaryOperator.identity());

        globalConfig.getJavadocConfig()
                .setAuthor("lhl")
        ;
        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.test");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                //.setGenerateSchema("audit")
                //.setTablePrefix("au_")
                .setGenerateTable("template_file")
        ;

        globalConfig.enableController()
                .setRestStyle(true);
        globalConfig.enableService();

        globalConfig.enableServiceImpl()
                .isCacheExample();

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true);

        //设置生成 mapper
        globalConfig.enableMapper();

        //可以单独配置某个列
        /*ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.getStrategyConfig()
                .setColumnConfig("account", columnConfig);*/


        return globalConfig;
    }
}
